package com.woyowodaows.adapter.mine;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.TextView;


import com.woyowodaows.R;
import com.woyowodaows.activity.mine.DetailedAddressActivity;
import com.woyowodaows.application.MyApplication;
import com.woyowodaows.base.BaseBean;
import com.woyowodaows.bean.mine.AddressInfosBean;
import com.woyowodaows.dialog.YNDialog;
import com.woyowodaows.request.HttpAsyncTask;
import com.woyowodaows.request.HttpRequestListener;
import com.woyowodaows.request.InterfaceUrl;
import com.woyowodaows.utils.ToastUtil;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * 地址列表1
 */

public class AddressListAdapter extends RecyclerView.Adapter<AddressListAdapter.MyViewHolder> {

    private Context context;
    private Activity mActivity;
    private List<AddressInfosBean> addressList = new ArrayList<>();
    private int tempPosition = -1;
    OnItemClickListener onItemClickListener;
    private final static int REQUESTCODE = 1; // 返回的结果码

    private AddressInfosBean address;


    Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            switch (msg.what) {
                case 0:
                    ToastUtil.show_short(MyApplication.getApplicationContext(), "删除成功！");
                    notifyDataSetChanged();
//                    initCheck();
                    break;
                case 1:
//                    ToastUtil.show_short(MyApplication.getApplicationContext(), "设置成功！");
                    notifyDataSetChanged();
                    break;
            }
        }
    };

    public AddressListAdapter(Context context, Activity mActivity, List<AddressInfosBean> addressList, RecyclerView recycleView) {
        this.context = context;
        this.mActivity = mActivity;
        this.addressList = addressList;
//        initCheck();
    }

    private void initCheck() {
        for (int i = 0; i < addressList.size(); i++) {
//            if (addressList.get(i).getIsDefault().equals("1")) {
//                tempPosition = i;
//            }
        }
    }


    @Override
    public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(context).inflate(R.layout.item_address_list, parent, false);
        return new MyViewHolder(view);
    }

    @Override
    public void onBindViewHolder(final MyViewHolder holder, final int position) {
        address = addressList.get(position);
        holder.name.setText(address.getAreaNames());
        holder.phone.setText(address.getCreateTime());
//        湖南 长沙市 开福区 芙蓉北路 泊富国际广场 达摩蛋 达摩蛋
        holder.address.setText(address.getAddress());
//        if (address.getIsDefault().equals("1")) {
//            holder.defaultAds.setText("默认地址");
//            holder.defaultAds.setChecked(true);
//            holder.defaultAds.setTextColor(Color.parseColor("#ffffa625"));
//        } else if (address.getIsDefault().equals("2")) {
//            holder.defaultAds.setText("设为默认");
//            holder.defaultAds.setChecked(false);
//            holder.defaultAds.setTextColor(Color.parseColor("#ff666666"));
//        }

//        holder.defaultAds.setOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View view) {
//                tempPosition = position;
//                updateAddress();
//
//            }
//        });
//        holder.view.setOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View view) {
//                onItemClickListener.onItemClick(address);
//            }
//        });

    }

//    //7.修改用户默认地址
//    private void updateAddress() {
//        AddressInfosBean bean = addressList.get(tempPosition);
//        Map<String, Object> mparam = new HashMap<String, Object>();
//        mparam.put("id", bean.getId());
//        HttpAsyncTask.getInstance().onPostParam(context, "正在修改默认地址...", true, InterfaceUrl.UPDATE_CHANGE_USERADDRESS, UserDataBean.class, mparam, new HttpRequestListener() {
//            @Override
//            public void onSuccess(Object result) {
//                if (result != null) {
//                    UserDataBean bean = (UserDataBean) result;
//                    if (bean.getResult() == HttpAsyncTask.SUCCESS) {
//                        for (int i = 0; i < addressList.size(); i++) {
//                            addressList.get(i).setIsDefault("2");
//                        }
//                        addressList.get(tempPosition).setIsDefault("1");
//                        notifyDataSetChanged();
//                        ToastUtil.show_short(MyApplication.getContext(), "设置成功！");
//                    } else {
//                        ToastUtil.show(context, bean.getMessage());
//                    }
//                }
//            }
//
//            @Override
//            public void onError(String message) {
//                ToastUtil.show(context, message);
//            }
//        });
//    }

    @Override
    public int getItemCount() {
        return addressList.size();
    }

    class MyViewHolder extends RecyclerView.ViewHolder {
        @BindView(R.id.tv_address_name)
        TextView name;
        @BindView(R.id.tv_address_phone)
        TextView phone;
        @BindView(R.id.tv_address_ads)
        TextView address;
        @BindView(R.id.cb_address_default)
        CheckBox defaultAds;
        @BindView(R.id.tv_address_edit)
        TextView edit;
        @BindView(R.id.tv_address_delete)
        TextView delete;
        View view;

        @OnClick(R.id.tv_address_edit)
        public void onEdit() {
            Intent intent = new Intent(context, DetailedAddressActivity.class);
            Bundle bundle = new Bundle();
            bundle.putString("ATYPE", "editAdd");
            bundle.putSerializable("ADDRESS", addressList.get(getAdapterPosition()));
            intent.putExtras(bundle);
            mActivity.startActivityForResult(intent, REQUESTCODE); //REQUESTCODE--->1


        }

        @OnClick(R.id.tv_address_delete)
        public void onDelete() {
            YNDialog dialog = new YNDialog(context);
            dialog.setMessageText("确认要删除该地址？");
            dialog.setDialogListener(new YNDialog.OnDialogListener() {
                @Override
                public void onCancelClick(View view) {
                }

                @Override
                public void onSureClick(View view) {
                    deleteAddress(addressList.get(getAdapterPosition()).getId()+"", getAdapterPosition());

                }
            });
            dialog.show();


        }

        public MyViewHolder(View view) {
            super(view);
            ButterKnife.bind(this, view);
            this.view = view;

        }
    }

    public interface OnItemClickListener {
        void onItemClick(AddressInfosBean bean);
    }

    public void setOnItemClickListener(OnItemClickListener onItemClickListener) {
        this.onItemClickListener = onItemClickListener;
    }

    private void deleteAddress(String id, final int position) {
        Map<String, Object> mparam = new HashMap<String, Object>();
        mparam.put("id", id);
        HttpAsyncTask.getInstance().onPostParam(context, "删除中...", true, InterfaceUrl.DELETEUSERADDRESS, BaseBean.class, mparam, new HttpRequestListener() {
            @Override
            public void onSuccess(Object result) {
                if (result != null) {
                    BaseBean bean = (BaseBean) result;
                    if (bean.getResult() == HttpAsyncTask.SUCCESS) {
                        addressList.remove(position);
//                        if (tempPosition != -1) {
//                            tempPosition = -1;
//                        }
                        handler.sendEmptyMessage(0);
                    } else {
                        ToastUtil.show(context, bean.getMessage());
                    }
                }
            }

            @Override
            public void onError(String message) {
                ToastUtil.show(context, message);
            }
        });
    }


}
